module amenu
import StdEnv, japi

/* This example shows how to create a menu. 
   It also shows how to create a non-modal dialog. 
   For modal dialog, see the adialog.prj example.
   There is also an example of popup menu. */

:: State= { frame::!Framer, dialog::!Container,
            open::!Entry, quit::!Entry, 
            fText::!TDev, dText::!TDev,
            quitDialog::!TDev, 
            popupMenu::!Menu, 
            popupQuit::!Entry}

factorial n
  | n < 1= 1
  | otherwise= n* (factorial (n-1))
  
strfactorial dv
  # n= toInt(jgetText dv) 
  = jsetText dv (toString(factorial n))

upper txt= jsetText txt {toUpper c \\ c <-: jgetText txt}  

Start world
 # (js, world) = begin_japi world
   (frm, js) = jnewFrame  (WH 400 350) "Factorial" js
   (txt, js) = jnewField(XY 80 250) 30 frm js
   (dlg, js) = jnewDialog(XY 30 60) "Uppercase" frm js
   (dlg, js) = jverticalLayout dlg js
   (dlg, js) = vGap dlg 20 js
   (tx, js) = jnewField FLOW 20 dlg js
   (ok, js) = jnewButton FLOW "Ok" dlg js
   (mnb, js) = jnewBar frm js
   (filemn, js) = jnewMenu "File" mnb js 
   (opn, js) = jnewItem   "Open" filemn js
   (ppup, js) = jnewPopup "Hi!" frm js
   (popquit, js) = jnewItem "Really Quit" ppup js
   (notyet, js)= jnewItem "Continue" ppup js
    
   (qt, js) = jnewItem  "Quit" filemn js
    
   (dlg, js)= jpack dlg js
   (frm, js) = jshow frm js
   s= {  frame= frm, dialog= dlg, 
         open= opn, quit= qt, 
         popupMenu= ppup,  popupQuit= popquit, 
         fText= txt, dText= tx, quitDialog= ok}
   js= loop s js
  = end_japi js world
where
  loop :: !State !JapiState -> JapiState
  loop s js
    # (na, js) = jnextaction js
    | na== (act s.frame)  = jquit js
    | na== (act s.quit)
          # (p, js)= jshowpopup(50,80) s.popupMenu js
          = loop {s& popupMenu=p} js
    | na== (act s.fText)
              # ans= strfactorial s.fText 
              = loop {s& fText=ans} js
    | na== (act s.popupQuit) = jquit js
    | na== (act s.open)
              # (d, js) = jshow s.dialog js 
              = loop {s&dialog= d} js
    | na== (act s.quitDialog)
                # (d, js) = jhide s.dialog js
                = loop {s&dialog= d} js
    | na== (act s.dText)
               = loop {s&dText= upper s.dText} js
    | otherwise= loop s js
    
    
 